package com.ey12.service;

import java.util.List;

import com.ey12.db.dao.GmManagerDAO;
import com.ey12.db.domain.GmManager;
import com.ey12.db.domain.GmNews;
import com.ey12.db.domain.GmUser;
import com.ey12.utils.DateUtil;
import com.ey12.utils.MD5Util;

public class GmManagerService {
	
	private GmManagerDAO gmManagerDAO;

	public GmManagerDAO getGmManagerDAO() {
		return gmManagerDAO;
	}

	public void setGmManagerDAO(GmManagerDAO gmManagerDAO) {
		this.gmManagerDAO = gmManagerDAO;
	}
	
	public List getManagerByType(String userType) {
		List list = gmManagerDAO.findByUserType(userType);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}
	public List getAll(){
		return gmManagerDAO.findAll();
	}
	public void deleteGmManager(GmManager gm) {
		System.out.println(gm.getRealname());
		gmManagerDAO.delete(gm);
	}
	public void update(GmManager gm) {
		String pwd=MD5Util.toMD5(gm.getPassword());
		gm.setPassword(pwd);
		System.out.println(gm.getRealname());
		gmManagerDAO.saveOrUpdate(gm);
	}
	public void save(GmManager gm) {
		gm.setId(null);
		String pwd=MD5Util.toMD5(gm.getPassword());
		gm.setPassword(pwd);
		gm.setTotalOrder(new Integer(0));
		gm.setWaitOrder(new Integer(0));
		gm.setIsFree(new Integer(3));
		gmManagerDAO.saveOrUpdate(gm);
	}
	
	public GmManager checkAndUpdatePass(String username, String password,
			String newpassword) {
		List list = gmManagerDAO.findByUserName(username);
		if (list.size() > 0) {
			GmManager user = (GmManager) list.get(0);
			if ((user.getPassword().equals(MD5Util.toMD5(password)))) {
				user.setPassword(MD5Util.toMD5(newpassword));
				gmManagerDAO.saveOrUpdate(user);
				return user;
			} else {
				return null;
			}
		}
		return null;
	}
	
	public GmManager loginManager(String username,String password){
		List list=gmManagerDAO.findByUserName(username.trim());
		System.out.println(list.size()+"-"+username+"-"+password);
		if(list.size()>0){
			GmManager mgr=(GmManager)list.get(0);
			if(mgr.getPassword().equals(MD5Util.toMD5(password))){
				System.out.println(list.size()+"-"+mgr.getUsername()+"-"+mgr.getPassword()+"-"+MD5Util.toMD5(password));				
				return mgr;
			}
		}
		return null;
	}
	

}
